import { User } from 'libs/db/entity/user.entity';
import { ApiProperty, ApiExtraModels, getSchemaPath } from "@nestjs/swagger";
import { IsNumber, IsOptional, IsEmpty, ValidateNested, IsString } from "class-validator";
import { Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn, Column } from "typeorm";
import { Area } from "./area.entity";
import { Village } from "./village.entity";
import { CrudValidationGroups } from "@nestjsx/crud";
import { BaseEntity } from "./base.entity";

const { CREATE, UPDATE } = CrudValidationGroups;


@Entity()
export class Town extends BaseEntity {

  @ApiProperty({
    description: '镇主键',
    required: false
  })
  @PrimaryGeneratedColumn()
  id ?: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: false,
    type: Number
  })
  @OneToMany(() => Village, Village => Village.town,{
    cascade: true
  })
  village : Village []

  @ApiProperty({
    example: '镇名称',
    description: '镇名称',
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  name : string;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => Area)
      }
    ]
  })
  // @ValidateNested({ always: true })
  @ManyToOne(() => Area, Area => Area.town, {
    cascade: true,
    eager: true
  })
  area :  Number | Area

  @ApiProperty({
    example: 1,
    description: '镇级管理员',
    required: false,
    type: Number
  })
  @OneToMany(() => User, User => User.town)
  users : User []
}